# GNAFU - "Ground, Navy and Air Fuck-Up" real-time strategy game
# Copyright (C) 2008-2009 k0wax <k0waxk0wax@gmail.com>
#
# This file is part of GNAFU.
#
# GNAFU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNAFU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNAFU.  If not, see <http://www.gnu.org/licenses/>.

"""Input module"""
# XXX READ-ONLY module XXX
from __future__ import division, absolute_import
import sys
import pyglet
from src import conf, vars
from src import game
from src.consts import *

class Mouse(object):
    def __init__(self):
        self.x, self.y = 0, 0
        self.buttons = None
        self.modiers = None
    @property
    def cx(self):
        return ((self.x - game.vp.x_shift) // vars.tile_size) % game.field.w
    @property
    def cy(self):
        return ((self.y - game.vp.y_shift) // vars.tile_size) % game.field.h
    def handle_drag(self, shit):
        pass
        #XXX lots of code here :(

class KeyBoard(object):
    def __init__(self):
        self.keymap = conf.vars['keymap']
        self.actions_map = { #fixme
            'quit': lambda: game.win.close,
            'scroll_up': (game.vp.scroll, DIR_N),
            'scroll_down': (game.vp.scroll, DIR_S),
            'scroll_left': (game.vp.scroll, DIR_W),
            'scroll_right': (game.vp.scroll, DIR_E),
            'fps_display_toggle': lambda: game.hud.toggle_fps_display,
            #'save_screenshot': game.win.save_screenshot,
            #'save_game': savegame.save,
         }
    def handle_key(self, key):
        if key not in self.keymap:
            print 'No such key'
            return
        action_name = self.keymap[key]
        print 'action_name', action_name
        #self.actions_map[action_name]()
        if action_name == 'quit':
            sys.exit()
        elif action_name == 'scroll_up':
            game.vp.scroll(DIR_N)
        elif action_name == 'scroll_down':
            game.vp.scroll(DIR_S)
        elif action_name == 'scroll_right':
            game.vp.scroll(DIR_E)
        elif action_name == 'scroll_left':
            game.vp.scroll(DIR_W)
        elif action_name == 'save_game':
            game.save()
        elif action_name == 'load_game':
            game.load()

# init
if 'initialized' not in globals():
    globals()['m'] = Mouse()
    globals()['kb'] = KeyBoard()
    globals()['initialized'] = True
